<?php

require_once __DIR__.'/vendor/autoload.php';

// 加载环境变量
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

// 创建数据库连接
try {
    $pdo = new PDO(
        "mysql:host={$_ENV['DB_HOST']};dbname={$_ENV['DB_DATABASE']};charset=utf8mb4",
        $_ENV['DB_USERNAME'],
        $_ENV['DB_PASSWORD'],
        [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        ]
    );

    echo "✅ 数据库连接成功\n";

    // 检查roles表
    $rolesResult = $pdo->query("SELECT id, code, name FROM roles");
    $roles = $rolesResult->fetchAll();

    echo "\n=== 角色列表 ===\n";
    foreach ($roles as $role) {
        echo "ID: {$role['id']}, Code: {$role['code']}, Name: {$role['name']}\n";
    }

    // 检查users表中的用户角色关联
    $userRolesResult = $pdo->query("
        SELECT u.username, r.code as role_code, r.name as role_name
        FROM users u
        LEFT JOIN user_roles ur ON u.id = ur.user_id
        LEFT JOIN roles r ON ur.role_id = r.id
    ");
    $userRoles = $userRolesResult->fetchAll();

    echo "\n=== 用户角色关联 ===\n";
    foreach ($userRoles as $userRole) {
        echo "用户: {$userRole['username']}, 角色: {$userRole['role_code']} ({$userRole['role_name']})\n";
    }

} catch (Exception $e) {
    echo "❌ 错误: " . $e->getMessage() . "\n";
}
